home *** CD-ROM | disk | FTP | other *** search
/ Collection of Tools & Utilities / Collection of Tools and Utilities.iso / graphic / frasr182.zip / JIIM.C < prev    next >
C/C++ Source or Header  |  1993-07-31  |  37KB  |  1,551 lines

  1. /*
  2.  * JIIM.C
  3.  *
  4.  * Generates Inverse Julia in real time, lets move a cursor which determines
  5.  * the J-set.
  6.  *
  7.  *  The J-set is generated in a fixed-size window, a third of the screen.
  8.  *
  9.  * This module is linked as an overlay, use ENTER_OVLY and EXIT_OVLY.
  10.  *
  11.  *
  12.  * The routines used to set/move the cursor and to save/restore the
  13.  * window were "borrowed" from editpal.c (TW - now we *use* the editpal code)
  14.  *     (if you don't know how to write good code, look for someone who does)
  15.  *
  16.  *    JJB  [jbuhler@gidef.edu.ar]
  17.  *    TIW  Tim Wegner
  18.  *    MS   Michael Snyder
  19.  *    KS   Ken Shirriff
  20.  * Revision History:
  21.  *
  22.  *        7-28-92       JJB  Initial release out of editpal.c
  23.  *        7-29-92       JJB  Added SaveRect() & RestoreRect() - now the
  24.  *                           screen is restored after leaving.
  25.  *        7-30-92       JJB  Now, if the cursor goes into the window, the
  26.  *                           window is moved to the other side of the screen.
  27.  *                           Worked from the first time!
  28.  *        10-09-92      TIW  A major rewrite that merged cut routines duplicated
  29.  *                           in EDITPAL.C and added orbits feature.
  30.  *        11-02-92      KS   Made cursor blink
  31.  *        11-18-92      MS   Altered Inverse Julia to use MIIM method.
  32.  *      11-25-92    MS   Modified MIIM support routines to better be
  33.  *                 shared with stand-alone inverse julia in
  34.  *                 LORENZ.C, and to use DISKVID for swap space.
  35.  *        05-05-93      TIW  Boy this change file really got out of date.
  36.  *                 Added orbits capability, various commands, some 
  37.  *                  of Dan Farmer's ideas like circles and lines 
  38.  *                 connecting orbits points.
  39.  */
  40.  
  41. #include <stdio.h>
  42. #include <stdlib.h>
  43. #include <string.h>
  44.  
  45. #ifndef XFRACT
  46. #include <stdarg.h>
  47. #include <dos.h>     /* for FP_SEG & FP_OFF */
  48. #else
  49. #include <varargs.h>
  50. #endif
  51.  
  52. #include <math.h>
  53.  
  54. #ifdef __TURBOC__
  55. #   include <mem.h>   /* to get mem...() declarations */
  56. #endif
  57.  
  58. #include "helpdefs.h"
  59. #include "fractint.h" /* for overlay stuff */
  60. #include "fractype.h"
  61. #include "prototyp.h"
  62.  
  63. #define FAR_RESERVE     8192L     /* amount of far mem we will leave avail. */
  64. #define MAXRECT         1024      /* largest width of SaveRect/RestoreRect */
  65.  
  66. #define newx(size)     mem_alloc(size)
  67. #define delete(block)
  68.  
  69. enum stored_at_values
  70.    {
  71.    NOWHERE,
  72.    DISK,
  73.    MEMORY
  74.    } ;
  75.  
  76. /* borrowed from LORENZ.C */
  77. struct affine
  78. {
  79.    /* weird order so a,b,e and c,d,f are vectors */
  80.    double a;
  81.    double b;
  82.    double e;
  83.    double c;
  84.    double d;
  85.    double f;
  86. };
  87.  
  88. extern int  setup_convert_to_screen(struct affine *);
  89.  
  90. int show_numbers =0;              /* toggle for display of coords */
  91. int stored_at;
  92. char far *memory = NULL;            /* pointer to saved rectangle */
  93. FILE *file;
  94. int windows = 0;               /* windows management system */
  95.  
  96. extern char scrnfile[];   /* file where screen portion is */
  97.                   /* stored */
  98. extern BYTE      *line_buff;   /* must be alloced!!! */
  99. extern int           sxdots;             /* width of physical screen         */
  100. extern int           sydots;             /* depth of physical screen         */
  101. extern int           sxoffs;             /* start of logical screen          */
  102. extern int           syoffs;             /* start of logical screen          */
  103. extern int           strlocn[];          /* 10K buffer to store classes in   */
  104. extern int           colors;             /* # colors avail.                  */
  105. extern int       using_jiim;
  106. extern int       viewwindow;     /* 0 for full screen, 1 for window */
  107. extern float     viewreduction;  /* window auto-sizing */
  108. extern int       viewcrop;               /* nonzero to crop default coords */
  109. extern float     finalaspectratio;              /* for view shape and rotation */
  110. extern int       viewxdots,viewydots;   /* explicit view sizing */
  111. extern double    dxsize, dysize;
  112. extern int       xdots;                  /* width of logical screen          */
  113. extern int       ydots;                  /* depth of logical screen          */
  114. extern double    xxmax,xxmin;    /* limits of the "window"       */
  115. extern double    yymax,yymin;    /* on the z-plane               */
  116. extern int           color_bright;       /* brightest color in palette       */
  117. extern int           color_dark;         /* darkest color in palette         */
  118. extern int           color_medium;       /* nearest to medbright gray color  */
  119. extern int           lookatmouse;        /* mouse mode for getakey(), etc    */
  120. extern int           maxit;                  /* try this many iterations */
  121. extern int           fractype;       /* fractal type */
  122. extern _CMPLX     old,new,init;
  123. extern double tempsqrx,tempsqry;
  124. static int xc, yc;                       /* corners of the window */
  125. static int xd, yd;                       /* dots in the window    */
  126. extern void displayc(int x, int y, int fg, int bg, int ch);
  127. extern float screenaspect;
  128. double xcjul = BIG;
  129. double ycjul = BIG;
  130. extern int hasinverse;
  131.  
  132. void displays(int x, int y, int fg, int bg, char *str, int len)
  133. {
  134.    int i;
  135.    for(i=0;i<len; i++)
  136.       displayc(x+i*8, y, fg, bg, str[i]);
  137. }
  138.  
  139. /* circle routines from Dr. Dobbs June 1990 */
  140. int xbase, ybase;
  141. unsigned xAspect, yAspect;
  142.  
  143. void SetAspect(double aspect)
  144. {
  145.    xAspect = 0;
  146.    yAspect = 0;
  147.    aspect = fabs(aspect);
  148.    if (aspect != 1.0)
  149.       if (aspect > 1.0)
  150.      yAspect = 65536.0 / aspect;
  151.       else
  152.      xAspect = 65536.0 * aspect;
  153. }
  154.  
  155. void _fastcall c_putcolor(int x, int y, int color)
  156.    {
  157.    /* avoid writing outside window */
  158.    if ( x < xc || y < yc || x >= xc + xd || y >= yc + yd )
  159.       return ;
  160.    if(y >= sydots - show_numbers) /* avoid overwriting coords */
  161.       return;
  162.    if(windows == 2) /* avoid overwriting fractal */
  163.       if (0 <= x && x < xdots && 0 <= y && y < ydots)
  164.      return;
  165.    putcolor(x, y, color);
  166.    }
  167.  
  168.  
  169. int  c_getcolor(int x, int y)
  170.    {
  171.    /* avoid reading outside window */
  172.    if ( x < xc || y < yc || x >= xc + xd || y >= yc + yd )
  173.       return 1000;
  174.    if(y >= sydots - show_numbers) /* avoid overreading coords */
  175.       return 1000;
  176.    if(windows == 2) /* avoid overreading fractal */
  177.       if (0 <= x && x < xdots && 0 <= y && y < ydots)
  178.      return 1000;
  179.    return getcolor(x, y);
  180.    }
  181.  
  182. void circleplot(int x, int y, int color)
  183. {
  184.    if (xAspect == 0)
  185.       if (yAspect == 0)
  186.      c_putcolor(x+xbase, y+ybase,color);
  187.       else
  188.      c_putcolor(x+xbase, (short)(ybase + (((long) y * (long) yAspect) >> 16)),color);
  189.    else
  190.       c_putcolor((int)(xbase + (((long) x * (long) xAspect) >> 16)), y+ybase, color);
  191. }
  192.  
  193. void plot8(int x, int y, int color)
  194. {
  195.    circleplot(x,y,color);
  196.    circleplot(-x,y,color);
  197.    circleplot(x,-y,color);
  198.    circleplot(-x,-y,color);
  199.    circleplot(y,x,color);
  200.    circleplot(-y,x,color);
  201.    circleplot(y,-x,color);
  202.    circleplot(-y,-x,color);
  203. }
  204.  
  205. void circle(int radius, int color)
  206. {
  207.    int x,y,sum;
  208.  
  209.    x = 0;
  210.    y = radius << 1;
  211.    sum = 0;
  212.  
  213.    while (x <= y)
  214.    {
  215.       if ( !(x & 1) )   /* plot if x is even */
  216.      plot8( x >> 1, (y+1) >> 1, color);
  217.       sum += (x << 1) + 1;
  218.       x++;
  219.       if (sum > 0)
  220.       {
  221.      sum -= (y << 1) - 1;
  222.      y--;
  223.       }
  224.    }
  225. }
  226.  
  227.  
  228. /*
  229.  * MIIM section:
  230.  *
  231.  * Global variables and service functions used for computing
  232.  * MIIM Julias will be grouped here (and shared by code in LORENZ.C)
  233.  *
  234.  */
  235.  
  236.  
  237.  long   ListFront, ListBack, ListSize;  /* head, tail, size of MIIM Queue */
  238.  long   lsize, lmax;            /* how many in queue (now, ever) */
  239.  int    maxhits = 1;
  240.  int    OKtoMIIM;
  241.  int    SecretExperimentalMode;
  242.  float  luckyx = 0, luckyy = 0;
  243.  extern int debugflag;
  244.  extern int           bitshift;         /* bit shift for fudge */
  245.  extern long          fudge;            /* fudge factor (2**n) */
  246.  
  247.  
  248. long lsqrt(long f)
  249. {
  250.     int N;
  251.     unsigned long y0, z;
  252.     static long a=0, b=0, c=0;                  /* constant factors */
  253.  
  254.     if (f == 0)
  255.     return f;
  256.     if (f <  0)
  257.     return 0;
  258.  
  259.     if (a==0)                                   /* one-time compute consts */
  260.     {
  261.     a = fudge * .4